/**
 * 分析
 */
export default class Analysis {
    constructor(viewer) {
        this.viewer = viewer;
        this.camera = viewer.camera;
        this.scene = viewer.scene;

        // 分析出来的 entity
        this.analysisEntities = [];
    }
    

    // 限高分析
    limitHeight() {

    }


    // 通视分析
    visibilityAnalysis(entity, positions) {

        // 排除碰撞监测的对象
        let objectsToExclude = [entity];

        // 计算射线的方向
        let direction = Cesium.Cartesian3.normalize(
            Cesium.Cartesian3.subtract(
                positions[1],
                positions[0],
                new Cesium.Cartesian3()
            ),
            new Cesium.Cartesian3()
        );
        // 建立射线
        let ray = new Cesium.Ray(positions[0], direction);
        // 计算交互点，返回第一个
        let result = this.viewer.scene.pickFromRay(ray, objectsToExclude);

        if (result !== undefined && result !== null) {
            // 可视区域
            let line1 = this.drawLine(result.position, positions[0], Cesium.Color.GREEN); 
            this.analysisEntities.push(line1)
            // 不可视区域
            let line2 = this.drawLine(result.position, positions[1], Cesium.Color.RED); 
            this.analysisEntities.push(line2)
        } else {
            let line3 = this.drawLine(positions[0], positions[1], Cesium.Color.GREEN);
            this.analysisEntities.push(line3)
        }
        let entities = [...this.analysisEntities]
        this.analysisEntities = [];
        return entities
    }

    /**
     * 
     * @param {Cartesian3} p1 点1
     * @param {Cartesian3} p2 点2
     * @param {*} color 线段颜色
     * @returns 
     */
    drawLine(p1, p2, color) {
        return this.viewer.entities.add({
            polyline: {
                positions: [p1, p2],
                width: 1,
                material: color,
                depthFailMaterial: color
            }
        });
    }

    // 可视域分析
    visualFieldAnalysis() {

    }
}